const app = getApp()
const WXAPI = require('../../wxapi/main')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    partUserInfo: {},
    reqId: '',
    reqDetail: {},
    taskDetailList: [],
    isTaskNewFlag: true,
    status: '', //published 已发布 completed 
  },


  // 滑动到最底部
  scrollToBottom() {
    setTimeout(() => {
      wx.pageScrollTo({
        scrollTop: 200000,
        duration: 3
      });
    }, 600)
  },
  changeStatus: function () {
    let that = this
    let _reqStatus = that.data.status == 'published' ? 'completed' : 'published'
    let _content = that.data.status == 'published' ? '确认该需求已经达成目标？' : '确认该需求未达成目标？'
    wx.showModal({
      title: '状态变更',
      content: _content,
      success(res) {
        if (res.confirm) {
          let postData = { reqId: that.data.reqId, reqStatus: _reqStatus }
          console.log('post data ', postData)
          WXAPI.completeReq(postData)
            .then(res => {
              console.log('res ', res)
              if (res.code === 0) {
                wx.showToast({
                  title: '更新成功',
                  icon: 'success'
                })
                that.onShow()
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'error'
                })
              }
            })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  loadTask: function (_reqId) {
    var that = this
    WXAPI.loadTaskByReqId({ reqId: _reqId })
      .then(function (res) {
        console.log('find task list: ', res.data)
        if (res.code === 0) {
          var _taskDetailList = res.data
          if (_taskDetailList.length <= 0) {
            that.setData({
              isTaskNewFlag: true
            })
          } else {
            that.setData({
              taskDetailList: _taskDetailList,
              isTaskNewFlag: false
            })
          }
        } else {
          // 查询出错了
        }
      })
  },

  loadDatas: function (_reqId) {
    var that = this;
    WXAPI.loadReqOne({ reqId: _reqId })
      .then(function (res) {
        console.log('find req detail: ', res)
        if (res.code === 0) {
          let _reqDetail = res.data
          that.setData({
            reqDetail: _reqDetail,
            status: _reqDetail.reqStatus
          })
          console.log(_reqDetail)
        } else {
          wx.showModal({
            title: '错误',
            content: '没有找到该商品',
            showCancel: false,
          })
          return
        }
      })
  },

  taskDetail: function (e) {
    console.log('明细 ', e)
    var that = this
    var id = e.currentTarget.dataset.id;
    console.log('ready enter into req detail page ', id)
    WXAPI.resetWordCount({taskId: id}).then(res => {
      console.log(res.code)
      if (res.code === 0) {
        // 清除ok
        wx.navigateTo({
          url: "/pages/req/detail?reqId=" + that.data.reqId + "&taskId=" + id
        })
      }
    })
   
  },

  onLoad(e) {
    // console.log('on load', e)
    var that = this
    var _reqId = ''

    if (e.scene) {
      console.log('scene ', e.scene)
      _reqId = e.scene
      that.setData({ reqId: _reqId })
    } else {
      if (e.reqId === undefined || e.reqId === '') {
        wx.redirectTo({
          url: '/pages/error/index',
        })
        return
      }
      _reqId = e.reqId
      that.setData({ reqId: _reqId })
    }

    // 设置当前用户
    var _partUserInfo = wx.getStorageSync('partUserInfo')
    if (_partUserInfo) {
      that.setData({
        partUserInfo: _partUserInfo
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    var _reqId = that.data.reqId
    console.log('on show req id ', _reqId)
    that.loadDatas(_reqId)
    that.loadTask(_reqId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})